package com.youxue.service;

import com.youxue.dao.ClassMapper;
import com.youxue.entity.Class;
import com.youxue.entity.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 15c on 2018/1/20.
 */
@Service("Class")
public class ClassServiceImpl implements ClassService {

    @Autowired
    ClassMapper classMapper;

    @Override
    public List<Class> showATypeAllClassByPage(Integer startPos, Integer pageSize, Integer classTypeId) {
        return classMapper.selectATypeAllClassByPage(startPos, pageSize, classTypeId);
    }

    @Override
    public Class showAClassInfo(Integer classId) {
        return classMapper.selectByPrimaryKey(classId);
    }

    @Override
    public int showAClassTypeCount(Integer classTypeId) {
        return classMapper.selectAClassTypeCount(classTypeId);
    }

    @Override
    public List<Class> showListClassByKeyWord(String keyWord, Integer startPos, Integer pageSize) {
        if (keyWord != null && !"".equals(keyWord)) {
            keyWord = "%" + keyWord + "%";
        }
        return classMapper.selectListClassByKeyWord(keyWord, startPos, pageSize);
    }

    @Override
    public int showListClassCountByKeyWord(String keyWord) {
        if (keyWord != null && !"".equals(keyWord)) {
            keyWord = "%" + keyWord + "%";
        }
        return classMapper.selectListClassCountByKeyWord(keyWord);
    }

    @Override
    public List<Class> showListClassCountMore(Integer num) {
        return classMapper.selectListClassCountMore(num);
    }

    @Override
    public List<Class> selectOrderClassTimeByPage(Integer startNum,Integer offsetNum) {
        return classMapper.selectOrderClassTimeByPage(startNum,offsetNum);
    }

    @Override
    public List<Class> showListClassByTime(Integer num) {
        return classMapper.selectListClassByTime(num);
    }

    @Override
    public int reviseAClassCount(Integer classId) {
        return classMapper.updateAClassCount(classId);
    }

    @Override
    public int reviseAClassScore(Integer classId,Double score) {
        return classMapper.updateAClassScore(classId,score);
    }

    @Override
    public Pager<Class> showListClass(Integer pageOffset) {
        //一页多少条数据
        int pageSize = 6;
        Pager<Class> pager = new Pager<Class>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = classMapper.selectClassTotal();
        pager.setTotalRecord(totalRecord);
        pager.setDatas(classMapper.selectListClass(pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    @Override
    public int showClassTotal() {
        return classMapper.selectClassTotal();
    }

    @Override
    public Class showAClass(Integer classId) {
        return classMapper.selectAClass(classId);
    }

    @Override
    public int updateAclass(Class aclass) {
        return classMapper.updateByPrimaryKeySelective(aclass);
    }

    @Override
    public int findClassCountByRetrieval(String keyword) {

        if (keyword != null && !"".equals(keyword)) {
            keyword = "%" + keyword + "%";
        }
        return classMapper.selectClassTotalByKeyword(keyword);
    }

    @Override
    public Pager<Class> findClassByRetrieval(String keyword, Integer pageOffset) {

        if (keyword != null && !"".equals(keyword)) {
            keyword = "%" + keyword + "%";
        }

        //一页多少条数据
        int pageSize = 6;
        Pager<Class> pager = new Pager<Class>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = classMapper.selectClassTotalByKeyword(keyword);
        pager.setTotalRecord(totalRecord);
        pager.setDatas(classMapper.selectListClassByKeyword(keyword, pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    @Override
    public int addAclass(Class aclass) {
        return classMapper.insertSelective(aclass);
    }

    @Override
    public int selectMostStudyClassNum() {
        return classMapper.selectMostStudyClassNum();
    }

    @Override
    public List<Class> selectMostStudyClassByPage(Integer startNum, Integer offSetNum) {
        return classMapper.selectMostStudyClassByPage(startNum,offSetNum);
    }
}
